//
//  YConfig.m
//  ArtCircle
//
//  Created by sunbin on 2017/5/21.
//  Copyright © 2017年 HuaYue. All rights reserved.
//

#import "YConfig.h"

#import "ConfigHead.h"

NSString * const UID = @"UID";

@implementation YConfig
{
    BQLDBTool *tool ;
}
single_implementation(YConfig);

+ (void)saveOwnAccount:(NSString *)account andPassword:(NSString *)password
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:account ?: @"" forKey:kAccount];
    
    [userDefaults synchronize];
    [SSKeychain setPassword:password ?: @"" forService:kService account:account];
}


+ (NSArray *)getOwnAccountAndPassword
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *account = [userDefaults objectForKey:kAccount];
    NSString *password = [SSKeychain passwordForService:kService account:account] ?: @"";
    
    if (account) {return @[account, password];}
    return nil;
}

+ (void)clearCookie
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"sessionCookies"];
    [defaults removeObjectForKey:REGISTIONID];
}

#pragma make 个人uid
+ (void )saveOwnID:(NSString *)uid{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:uid forKey:UID];
    [userDefaults synchronize];
}


+ (int64_t)getOwnID
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults integerForKey:UID];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self sqlConfig];
    }
    
    return self;
}

- (void )sqlConfig {

    tool =  [BQLDBTool instantiateTool];

    NSDictionary *modelWithDictionary = @{@"brokerId":@"" ,
                          @"loginemail":@"" ,
                          @"permanentregion":@"" ,
                          @"unionid":@"" ,
                          @"openid":@"" ,
                          @"broker":@"" ,
                          @"del":@"" ,
                          @"delDtm":@"" ,
                          @"sex":@"" ,
                          @"nickname":@"" ,
                          @"constellation":@"" ,
                          @"query":@"" ,
                          @"personalsignature":@"" ,
                          @"hxPsw":@"" ,
                          @"signature":@"" ,
                          @"realname":@"" ,
                          @"fanscount":@"" ,
                          @"job":@"" ,
                          @"getnewid":@"" ,
                          @"headurl":@"" ,
                          @"token":@"" ,
                          @"label":@"" ,
                          @"phone":@"" ,
                          @"attentioncount":@"" ,
                          @"realnameon":@"" ,
                          @"pastdtm":@"" ,
                          @"openidapp":@"" ,
                          @"password":@"" ,
                          @"createdtm":@"" ,
                          @"username":@"" ,};

    ACUserModel *model = [ACUserModel modelWithDictionary:modelWithDictionary];
    [tool openDBWith:ACUserFile Model:model];
}


- (void)insertData:(NSDictionary *)data {
    ACUserModel *model = [ACUserModel modelWithDictionary:data];
    if([tool insertDataWith:ACUserFile Model:model]) {
        NSLog(@"插入成功");
    }
    else {
        NSLog(@"插入失败");

    }
}


- (void)deleteData {
    if([tool deleteDataWith:ACUserFile]) {
        NSLog(@"删除成功");
    }
    else {
        NSLog(@"删除失败");
    }

}


- (void)updateData:(NSDictionary *)data{
    ACUserModel *model = [ACUserModel modelWithDictionary:data];
    if([tool modifyDataWith:ACUserFile Model:model]) {
        NSLog(@"修改成功");
    }
    else {
        NSLog(@"修改失败");

    }
}


- (ACUserModel *)searchData {
    NSArray *array = [tool queryDataWith:ACUserFile];

   ACUserModel *model = [ACUserModel mj_objectWithKeyValues:[array lastObject]];
    
    return model;
}

- (void)updateNickName:(NSString *)nickName {

    NSDictionary *dic = [[tool queryDataWith:ACUserFile] lastObject];
    
    if([tool modifyDataWith:ACUserFile Key:@"nickname" Value:nickName Identifier:@"getNewid" IdentifierValue:dic[@"getNewid"]]) {
        NSLog(@"昵称修改成功");

    }
    else {
        NSLog(@"昵称修改失败");
    }

}









@end
